package device

import (
	"context"
	"device-admin/internal/app/web/service/common"
	"device-admin/internal/dal"
	"device-admin/internal/dao"
	"device-admin/internal/pkg/date"
	"strings"

	"192.168.1.75/go-pkg/errorx"
	"github.com/shopspring/decimal"
)

// GetAvgOnlineDevice 平均开机设备
func GetAvgOnlineDevice(
	ctx context.Context,
	merchantID string, // 指定的商户id
	locationID string, // 位置id，多个以逗号分隔
	startTime, endTime int64, // 开始和结束时间
) (
	avgOnlineDevice int, // 平均开机数
	avgOnlinePercent float64, // 平均开机百分比
	totalDevice int, // 设备总数
	totalOnlineDevice int, // 总的在线设备
	err error,
) {
	// 参数检查
	{
		if startTime <= 0 || endTime <= 0 {
			return 0, 0, 0, 0, errorx.New("无效的开始或结束时间", -1)
		}
	}

	// 获取商户路径
	merchantPathArr := []string{}
	merchantIDs := strings.Split(merchantID, ",")
	for _, id := range merchantIDs {
		merchantPath, _, err := common.GetMerchantInfo(ctx, id)
		if err != nil {
			continue
		}
		merchantPathArr = append(merchantPathArr, merchantPath)
	}
	if len(merchantPathArr) <= 0 {
		return 0, 0, 0, 0, errorx.New("无效的商户", -1)
	}

	// 获取这段时间内的天数
	dates := date.GetDates(startTime, endTime)
	// 获取商户下总设备数
	totalDevice = dao.Device{}.TotalDevice(ctx, dal.Q, strings.Join(merchantPathArr, ","), locationID)
	// 获取每日开机数
	dailyOnlineDevice := dao.DeviceActiveDuration{}.GetDailyOnlineDevice(ctx, dal.Q, strings.Join(merchantPathArr, ","), locationID, startTime, endTime)
	for _, data := range dailyOnlineDevice {
		totalOnlineDevice += int(data.Total)
	}
	// 平均开机设备
	if len(dates) == 0 {
		avgOnlineDevice = totalDevice
	} else {
		avgOnlineDevice = totalOnlineDevice / len(dates)
	}
	// 平均开机率
	if totalDevice == 0 {
		avgOnlinePercent = 0
	} else {
		avgOnlinePercent = float64(avgOnlineDevice*100) / float64(totalDevice)
		avgOnlinePercent, _ = decimal.NewFromFloat(avgOnlinePercent).Truncate(2).Float64()
		if avgOnlinePercent > 100 {
			avgOnlinePercent = 100
		}
	}
	return
}

// GetAvgActiveDevice 平均活跃设备
func GetAvgActiveDevice(
	ctx context.Context,
	merchantID string, // 用户的商户路径
	locationID string, // 多个以逗号分隔
	startTime, endTime int64, // 开机时间和截止时间
) (
	avgActiveDevice int, // 平均开机数
	avgActivePercent float64, // 平均开机百分比
	totalDevice int, // 设备总数
	totalActiveDevice int, // 总的活跃设备
	err error,
) {
	// 参数检查
	{
		if startTime <= 0 || endTime <= 0 {
			return 0, 0, 0, 0, errorx.New("无效的开始或结束时间", -1)
		}
	}
	// 获取商户路径
	merchantPathArr := []string{}
	merchantIDs := strings.Split(merchantID, ",")
	for _, id := range merchantIDs {
		merchantPath, _, err := common.GetMerchantInfo(ctx, id)
		if err != nil {
			continue
		}
		merchantPathArr = append(merchantPathArr, merchantPath)
	}
	if len(merchantPathArr) <= 0 {
		return 0, 0, 0, 0, errorx.New("无效的商户", -1)
	}
	// 获取这段时间内的天数
	dates := date.GetDates(startTime, endTime)
	// 商户下总设备数
	totalDevice = dao.Device{}.TotalDevice(ctx, dal.Q, strings.Join(merchantPathArr, ","), locationID)
	// 获取每日活跃设备
	dailyActiveDevice := dao.DeviceActiveDuration{}.GetDailyActiveDevice(ctx, dal.Q, strings.Join(merchantPathArr, ","), locationID, startTime, endTime)
	for _, data := range dailyActiveDevice {
		totalActiveDevice += int(data.Total)
	}
	// 平均活跃设备
	if len(dates) == 0 {
		avgActiveDevice = totalDevice
	} else {
		avgActiveDevice = totalActiveDevice / len(dates)
	}
	// 平均活跃率
	if totalDevice == 0 {
		avgActivePercent = 0
	} else {
		avgActivePercent = float64(avgActiveDevice*100) / float64(totalDevice)
		avgActivePercent, _ = decimal.NewFromFloat(avgActivePercent).Truncate(2).Float64()
		if avgActivePercent > 100 {
			avgActivePercent = 100
		}
	}
	return
}
